<?php

namespace App\Repositories\Admin;

use App\Models\Admin\CategoryModel;
use App\Repositories\BaseRepository;

class CategoryRepository extends BaseRepository
{
    protected $repository;

    public function __construct()
    {
        $this->repository = new CategoryModel();
    }

    /**
     * 操作分页获取
     */
    public function getIndexList()
    {
        return $this->repository->orderBy('id','asc')
            ->paginate(10);
    }

    /**
     * 父级
     */
    public function getParentList($pid=0)
    {
        $query = $this->repository->where('pid',0);
        if ($pid) { $query = $query->where('id',$pid); }
        return $query->orderBy('id','asc')->get();
    }

    /**
     * 子级
     */
    public function getSubList($pid)
    {
        return $this->repository->where('pid',$pid)
            ->orderBy('id','asc')
            ->get();
    }

    /**
     * 通过ID，获取名称
     */
    public function getCateNameById($cate)
    {
        $model = $this->repository->where('id',$cate)->first();
        return $model ? $model->name : '';
    }

    public function setAdd($data)
    {
        $data['created_at'] = time();
        return $this->repository->insertGetId($data);
    }

    public function setModify($data,$id)
    {
        $data['updated_at'] = time();
        return $this->repository->where('id',$id)
            ->update($data);
    }

    public function getShowOne($id)
    {
        return $this->repository->where('id',$id)->first();
    }
}